package com.xb.p40_usercontentprovider;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    TextView tv_context;
    EditText username;
    EditText phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_context=findViewById(R.id.tv_context);
        username=findViewById(R.id.username);
        phoneNum=findViewById(R.id.phoneNum);

    }

    public void clk_query(View view){
        //通过ID来查询一条记录，复用了姓名的输入框
        tv_context.setText("");
        String id = username.getText().toString();
        Uri uri = Uri.parse("content://com.xb.provider/user/"+id);
        Cursor cursor = getContentResolver().query(uri,null,null,null,null);
        printQueryResult(cursor);
        Toast.makeText(this, "联系人查询成功", Toast.LENGTH_SHORT).show();
    }


    public void clk_add(View view){
        String name=username.getText().toString();
        String phone=phoneNum.getText().toString();

        Uri uri=Uri.parse("content://com.xb.provider/user");
        ContentValues values=new ContentValues();
        values.put("username",name);
        values.put("phoneNum",phone);
        getContentResolver().insert(uri,values);
        Toast.makeText(this, "联系人数据添加成功", Toast.LENGTH_SHORT).show();
    }

    public void clk_update(View view){
        //1.根据名字查询到具体的ID
        String name=username.getText().toString();
        String phone=phoneNum.getText().toString();
        Uri uri=Uri.parse("content://com.xb.provider/user");

        ContentValues values=new ContentValues();
        values.put("phoneNum",phone);
        getContentResolver().update(uri,values,"username=?",new String[]{name});

        Toast.makeText(this, "联系人更新成功", Toast.LENGTH_SHORT).show();
    }

    public void clk_delete(View view){
        String name=username.getText().toString();
        Uri uri=Uri.parse("content://com.xb.provider/user");
        getContentResolver().delete(uri,"username=?",new String[]{name});
        Toast.makeText(this, "联系人删除成功", Toast.LENGTH_SHORT).show();
    }

    public void clk_queryall(View view){
        //URI的内容为Mainfest中配置的内容
        Uri uri=Uri.parse("content://com.xb.provider/user");
        Cursor cursor=getContentResolver().query(uri,null,null,null,null);
        printQueryResult(cursor);
    }

    public void printQueryResult(Cursor cursor){
        if (cursor!=null){
            tv_context.setText("");
            while (cursor.moveToNext()){
                String ID=cursor.getString(0);
                String ContactName=cursor.getString(1);
                String PhoneNum=cursor.getString(2);
                tv_context.append("联系人"+ID+": 姓名="+ContactName+", 电话="+PhoneNum+"\n");
            }
            cursor.close();
        }
    }
}

